package cn.supermaster.management.web.service.task.impl;

import cn.supermaster.management.common.exception.job.TaskException;
import cn.supermaster.management.common.page.TableDataInfo;
import cn.supermaster.management.common.response.WebResponse;
import cn.supermaster.management.common.utils.WebHelper;
import cn.supermaster.management.common.utils.poi.ExcelUtil;
import cn.supermaster.management.schedule.domain.ScheduleTask;
import cn.supermaster.management.schedule.quartz.CronUtils;
import cn.supermaster.management.schedule.service.IScheduleTaskService;
import cn.supermaster.management.web.service.task.ISTaskService;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.ModelMap;

import java.util.List;

/**
 * All rights Reserved, Designed By www.super-bytecode.com
 *
 * @ProjectName: supermaster-management
 * @Package: cn.supermaster.management.web.service.task.impl
 * @ClassName: ScheduleTaskServiceImpl
 * @Description: []
 * <p> </p>
 * @Author: [Lance Ting]
 * @Date: 2021/2/16 16:39
 * @Version: V1.0
 * @Copyright: 2021 www.super-bytecode.com Inc. All rights reserved.
 * TODO: 注意,本文件Lance Ting所作,如果转载或使用请标明具体出处!
 **/
@Service
public class STaskServiceImpl implements ISTaskService {

    private String prefix = "monitor/job";

    @Autowired
    private IScheduleTaskService scheduleTaskService;

    /*
     * ================================================
     *  页面跳转功能实现
     * ================================================
     */

    /**
     * @Description: [页面跳转首页]
     * <p></P>
     * @Title: skipScheduleTaskIndex
     * @Author: Lance Ting
     * @Date: 2021-02-16
     * @Return: java.lang.String
     */
    @Override
    public String skipScheduleTaskIndex() {
        return prefix + "/job";
    }

    /**
     * @Description: [跳转到详情界面]
     * <p></P>
     * @Title: skipScheduleTaskDetailsHtml
     * @Author: Lance Ting
     * @Date: 2021-02-16
     * @Param: modelMap
     * @Param: taskId
     * @Return: java.lang.String
     */
    @Override
    public String skipScheduleTaskDetailsHtml(ModelMap modelMap, Long taskId) {
        modelMap.put("name", "job");
        modelMap.put("job", scheduleTaskService.selectJobById(taskId));
        return prefix + "/detail";
    }

    /**
     * @Description: [新增调度界面]
     * <p></P>
     * @Title: skipAddScheduleTaskHtml
     * @Author: Lance Ting
     * @Date: 2021-02-16
     * @Return: java.lang.String
     */
    @Override
    public String skipAddScheduleTaskHtml() {
        return prefix + "/add";
    }

    /**
     * @Description: [修改调度任务界面]
     * <p></P>
     * @Title: skipModifyScheduleTaskHtml
     * @Author: Lance Ting
     * @Date: 2021-02-16
     * @Param: modelMap
     * @Param: taskId
     * @Return: java.lang.String
     */
    @Override
    public String skipModifyScheduleTaskHtml(ModelMap modelMap, Long taskId) {
        ScheduleTask scheduleTask = scheduleTaskService.selectJobById(taskId);
        modelMap.put("job", scheduleTask);
        return prefix + "/edit";
    }

    /*
     * ================================================
     *  Rest功能实现
     * ================================================
     */

    /**
     * @Description: [根据条件获取任务信息列表]
     * <p></P>
     * @Title: queryScheduleTaskByCondition
     * @Author: Lance Ting
     * @Date: 2021-02-16
     * @Param: task
     * @Return: cn.supermaster.management.common.page.TableDataInfo
     */
    @Override
    public TableDataInfo queryScheduleTaskByCondition(ScheduleTask task) {
        WebHelper.startPage();
        List<ScheduleTask> scheduleTasks = scheduleTaskService.selectJobList(task);
        TableDataInfo result = WebHelper.getDataTable(scheduleTasks);
        return result;
    }

    /**
     * @Description: [到处数据列表]
     * <p></P>
     * @Title: doExportScheduleTask
     * @Author: Lance Ting
     * @Date: 2021-02-16
     * @Param: task
     * @Return: cn.supermaster.management.common.response.WebResult
     */
    @Override
    public WebResponse doExportScheduleTask(ScheduleTask task) {
        List<ScheduleTask> list = scheduleTaskService.selectJobList(task);
        ExcelUtil<ScheduleTask> excelUtil = new ExcelUtil<ScheduleTask>(ScheduleTask.class);
        WebResponse webResponse = excelUtil.exportExcel(list, "定时任务");
        return webResponse;
    }

    /**
     * @Description: [删除定时任务]
     * <p></P>
     * @Title: removeScheduleTasks
     * @Author: Lance Ting
     * @Date: 2021-02-16
     * @Param: ids
     * @Return: cn.supermaster.management.common.response.WebResult
     */
    @Override
    public WebResponse removeScheduleTasks(String ids) {
        WebResponse webResponse = WebResponse.success();
        try {
            scheduleTaskService.deleteJobByIds(ids);
        } catch (SchedulerException e) {
            webResponse = WebResponse.failure("定时执行计划删除失败：" + e.getMessage());
        }
        return webResponse;
    }

    /**
     * @Description: [修改执行计划状态]
     * <p></P>
     * @Title: doChangeTaskStatus
     * @Author: Lance Ting
     * @Date: 2021-02-16
     * @Param: task
     * @Return: cn.supermaster.management.common.response.WebResult
     */
    @Override
    public WebResponse doChangeTaskStatus(ScheduleTask task) {
        WebResponse webResponse = WebResponse.success();
        try {
            int rows = scheduleTaskService.changeStatus(task);
            if (rows < 1) webResponse = WebResponse.failure();
        } catch (SchedulerException e) {
            WebResponse.failure("执行计划状态修改失败： " + e.getMessage());
        }
        return webResponse;
    }

    /**
     * @param task
     * @Description: [任务调度立即执行一次]
     * <p></P>
     * @Title: doScheduleTaskRunning
     * @Author: Lance Ting
     * @Date: 2021-02-19
     * @Param: task
     * @Return: cn.supermaster.management.common.response.WebResult
     */
    @Override
    public WebResponse doScheduleTaskRunning(ScheduleTask task) {
        WebResponse webResponse = WebResponse.success();
        try {
            scheduleTaskService.run(task);
        } catch (SchedulerException e) {
            WebResponse.failure("任务调度立即执行一次失败： " + e.getMessage());
        }
        return webResponse;
    }

    /**
     * @param task
     * @Description: [新增保存调度]
     * <p></P>
     * @Title: addScheduleTaskSave
     * @Author: Lance Ting
     * @Date: 2021-02-19
     * @Param: task
     * @Return: cn.supermaster.management.common.response.WebResult
     */
    @Override
    public WebResponse addScheduleTaskSave(ScheduleTask task) {
        WebResponse webResponse = WebResponse.success();
        try {
            if (!CronUtils.isValid(task.getCronExpression())) {
                return WebResponse.failure("cron表达式不正确");
            }
            int rows = scheduleTaskService.insertJob(task);
            if (rows < 1) {
                return WebResponse.failure("新增保存调度任务入库失败！");
            }
        } catch (SchedulerException | TaskException e) {
            WebResponse.failure("新增保存调度失败： " + e.getMessage());
        }
        return webResponse;
    }

    /**
     * @param task
     * @Description: [修改保存调度]
     * <p></P>
     * @Title: modifyScheduleTask
     * @Author: Lance Ting
     * @Date: 2021-02-19
     * @Param: task
     * @Return: cn.supermaster.management.common.response.WebResult
     */
    @Override
    public WebResponse modifyScheduleTask(ScheduleTask task) {
        WebResponse webResponse = WebResponse.success();

        try {
            if (!CronUtils.isValid(task.getCronExpression())) {
                return WebResponse.failure("cron表达式不正确");
            }
            int rows = scheduleTaskService.updateJob(task);
            if (rows < 1) {
                return WebResponse.failure("修改保存调度任务入库失败!");
            }
        } catch (SchedulerException | TaskException e) {
            WebResponse.failure("修改保存调度失败： " + e.getMessage());
        }
        return webResponse;
    }

    /**
     * @param task
     * @Description: [校验cron表达式是否有效]
     * <p></P>
     * @Title: checkCronExpressionValid
     * @Author: Lance Ting
     * @Date: 2021-02-19
     * @Param: task
     * @Return: boolean
     */
    @Override
    public boolean checkCronExpressionValid(ScheduleTask task) {
        return scheduleTaskService.checkCronExpressionIsValid(task.getCronExpression());
    }
}